package com.sj.parksdk.fragment.pay;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruffian.library.widget.RTextView;
import com.sj.parksdk.R;
import com.sj.parksdk.R2;
import com.sj.parksdk.core.BaseFragment;
import com.sj.parksdk.model.bean.PicPreInfo;
import com.sj.parksdk.net.http.MyNetApi;
import com.sj.parksdk.net.http.callback.TipRequestCallBack;
import com.sj.parksdk.net.request.MGetRequest;
import com.sj.parksdk.net.result.databean.Data_Order_Pics;
import com.sj.parksdk.net.result.databean.Data_PayRecord_Detail;
import com.sj.parksdk.setting.Extras;
import com.sj.parksdk.utils.Utils;
import com.sj.parksdk.utils.XToastUtils;
import com.xuexiang.xhttp2.callback.ProgressLoadingCallBack;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xrouter.annotation.AutoWired;
import com.xuexiang.xrouter.launcher.XRouter;
import com.xuexiang.xui.widget.imageview.preview.PreviewBuilder;
import com.xuexiang.xui.widget.statelayout.StatefulLayout;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.data.DateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Page(name = "缴费记录详情", params = {Extras.BASE_CODE})
public class FragmentPayRecordDetail extends BaseFragment {
    @BindView(R2.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R2.id.tv_pay_time)
    TextView tvPayTime;
    @BindView(R2.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R2.id.tv_address)
    TextView tvAddress;
    @BindView(R2.id.iv_pic)
    ImageView ivPic;
    @BindView(R2.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R2.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R2.id.tv_length)
    TextView tvLength;
    @BindView(R2.id.tv_price)
    TextView tvPrice;
    @BindView(R2.id.tv_title_price)
    TextView tvTitlePrice;
    @BindView(R2.id.tv_coupons)
    TextView tvCoupons;
    @BindView(R2.id.iv_coupons_next)
    ImageView ivCouponsNext;
    @BindView(R2.id.ll_coupons)
    LinearLayout llCoupons;
    @BindView(R2.id.tv_total)
    TextView tvTotal;
    @BindView(R2.id.bt_pay)
    RTextView btPay;
    @BindView(R2.id.tv_alredy_pay)
    TextView tvAlredyPay;
    @BindView(R2.id.ll_state)
    StatefulLayout llState;

    @AutoWired(name = Extras.BASE_CODE)
    String tradeNo;
    @BindView(R2.id.tv_car_number)
    TextView tvCarNumber;

    private String orderNumber ="";


    @Override
    protected void initArgs() {
        XRouter.getInstance().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.parksdk_fragment_pay_recorddetail;
    }

    @Override
    protected void initViews() {
        btPay.setVisibility(View.GONE);
        ivCouponsNext.setVisibility(View.GONE);
    }

    @Override
    protected void initListeners() {
        ivPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!StringUtils.isEmpty(orderNumber))queryOrderPic(orderNumber);
            }
        });
    }

    @Override
    protected void initData() {
        queryRecordDetail();
    }

    private void queryRecordDetail() {
        if (StringUtils.isEmpty(tradeNo)) {
            llState.showCustom(emptyStatus());
            return;
        }
        new MGetRequest(MyNetApi.ORDER_PAY_RECORD_DETAIL + tradeNo)
                .user()
                .execute(new ProgressLoadingCallBack<Data_PayRecord_Detail>(getProgressLoader()) {
            @Override
            public void onSuccess(Data_PayRecord_Detail response) throws Throwable {
                initRecordInfo(response);
            }
        });
    }

    private void initRecordInfo(Data_PayRecord_Detail data) {
        orderNumber = data.getOrderNo();

        tvCarNumber.setText(data.getCarNo());
        tvOrderNumber.setText(data.getOrderNo());
        tvPayTime.setText(DateUtils.millis2String(data.getPaidTime(),DateUtils.yyyyMMddHHmmss.get()));
        tvPayType.setText(data.getPayTypeName());
        tvTitlePrice.setText(Utils.getPriceHtml(data.getTotalFee().toString(),2));

        tvAddress.setText(data.getParkingLotName());
        tvStartTime.setText(DateUtils.millis2String(data.getChargeBeginTime(),DateUtils.yyyyMMddHHmmss.get()));
        tvEndTime.setText(DateUtils.millis2String(data.getChargeEndTime(),DateUtils.yyyyMMddHHmmss.get()));
        tvLength.setText(data.getChargeTime());
        tvPrice.setText(data.getTotalFeeStr());
        tvCoupons.setText(data.getDiscountFeeStr());
        tvTotal.setText(data.getPaidFeeStr());
        tvAlredyPay.setVisibility(View.VISIBLE);
    }

    private void queryOrderPic(String orderNo) {
        new MGetRequest(MyNetApi.PARK_ORDER_PICS + orderNo + "/pirtures")
                .user()
                .execute(new TipRequestCallBack<Data_Order_Pics>() {
                    @Override
                    public void onSuccess(Data_Order_Pics response) throws Throwable {
                        showPics(response);
                    }
                });
    }

    private void showPics(Data_Order_Pics response) {
        List<String> pic = new ArrayList<>();
        if(response.getInPirtures()!=null )pic.addAll(response.getInPirtures());
        if(response.getOutPirtures()!=null)pic.addAll(response.getOutPirtures());
        if(pic.size() == 0){
            XToastUtils.warning("暂无图片");
            return;
        }
        List<PicPreInfo> pics = new ArrayList<>();
        for (String url:pic
        ) {
            pics.add(new PicPreInfo(url));
        }
        PreviewBuilder.from(getActivity())
                .setImgs(pics)
                .setCurrentIndex(0)
                .setSingleFling(true)
                .setProgressColor(R.color.app_color_theme_main)
                .setType(PreviewBuilder.IndicatorType.Number)
                .start();

    }
}
